<?php

defined('_INSTALL') or die('Restricted access');

class MySQL {

    private $_host = '';
    private $_user = '';
    private $_password = '';
    private $_db = '';
    private $_link = null;

    public function  __construct($host, $user, $password, $db) {
        $this->setHost($host);
        $this->setUser($user);
        $this->setPassword($password);
        $this->setDatabase($db);
    }

    public function connect() {
        $this->_link = @mysql_connect($this->_host, $this->_user, $this->_password);
        if (!$this->_link) {
            throw new Exception('Could not connect: ' . mysql_error());
        }
        if (!@mysql_select_db($this->_db)) {
            throw new Exception("Unable to select $this->_db: " . mysql_error());
        }
    }

    public function setHost($host) {
        $this->_host = trim($host);
    }

    public function getHost() {
        return $this-_>host;
    }

    public function setUser($user) {
        $this->_user = trim($user);
    }

    public function getUser() {
        return $this->_user;
    }

    public function setPassword($password) {
        $this->_password = trim($password);
    }

    public function getPassword() {
        return $this->_password;
    }

    public function setDatabase($db) {
        $this->_db = trim($db);
    }

    public function getDatabase() {
        return $this->_db;
    }

    public function query($query, $answer = false) {
        if (is_resource($this->_link)) {
            $result = @mysql_query($query);
            if (!$result) {
                throw new Exception(mysql_errno($this->_link).' : '.mysql_error($this->_link)."\n");
            }
            if ($answer === true) {
                return $result;
            }
        }
    }

    public function disconnect() {
        if (is_resource($this->_link)) {
           @mysql_close($this->_link);
        }
    }
}
